 #include "gamelogic.h"
#include "stdlib.h"

void new_game(const size_t size)
{
    int i, j;

    gameField = (FIELD **)malloc(sizeof(FIELD *) * size);
    for( i = 0; i < size; i++) {
        gameField[i] = (FIELD *)malloc(sizeof(FIELD) * size);    
    }

    for(i = 0; i < size; i++) {
        for(j = 0; j < size; j++) {
            gameField[i][j] = FREE;
        }
    }

    fieldSize = size;

    isXmove = 1;
}

void end_game()
{
    int i;
    for(i = 0; i < fieldSize; i++) {
        free(gameField[i]);
    }
    free(gameField);
}


RESULT make_move(const unsigned int x, const unsigned int y)
{
    if(gameField[x][y])
        return BAD_MOVE;
    if(x < 0 || x >= fieldSize || y < 0 || y >= fieldSize)
        return BAD_MOVE;
    gameField[x][y] = isXmove ? X : O;   
}

RESULT check_line(const unsigned int *x, const unsigned int *y, 
                  const int dx, const int dy)
{
    int count;
    FIELD buf;

    int x_ = *x;
    int y_ = *y;

    buf = gameField[x_][y_];
    for(count = 0; 
        gameField[x_][y_] == buf && 
        x_ >= 0  && y_ >= 0 && x_ < fieldSize && y_ < fieldSize; 
        count++, x_+=dx, y_=+dy);
        
}